package com.czxy.service;

import com.czxy.dao.ProductMapper;
import com.czxy.domain.Product;
import com.czxy.utils.MyBatisUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;

public class ProductService {




    //根据商品id pid 查询商品信息
    public  Product findProductByPid(String pid){
        ProductMapper mapper = MyBatisUtils.getMapper(ProductMapper.class);
        //根据主键查询
        Product product = mapper.selectByPrimaryKey(pid);
        MyBatisUtils.close();
        return product;
    }



    //根据 分类id 分页查询商品信息
    public PageInfo getProductPageInfo(String cid ,int pageNum , int pageSize){

        //获取 Product  Mapper
        ProductMapper mapper = MyBatisUtils.getMapper(ProductMapper.class);
        //设置分页查询
        PageHelper.startPage(pageNum,pageSize);
        //根据cid 查询 商品信息  得到该分页的数据 list集合
        List<Product> list = mapper.findProductsByCid(cid);

        //把list集合  -> pageInfo

        PageInfo<Product> pageInfo = new PageInfo<>(list);

        //释放资源 返回pageInfo

        MyBatisUtils.close();
        return pageInfo;






    }
















    /**
     * 获取热门商品
     * @return
     */
    public List<Product> findHotProducts(){

        ProductMapper mapper = MyBatisUtils.getMapper(ProductMapper.class);
        List<Product> hotProducts = mapper.findHotProducts();
        MyBatisUtils.close();
        return hotProducts;
    }

    /**
     * 最新商品
     * @return
     */
    public List<Product> findNewProducts(){
        ProductMapper mapper = MyBatisUtils.getMapper(ProductMapper.class);
        List<Product> newProducts = mapper.findNewProducts();
        MyBatisUtils.close();
        return newProducts;
    }
}
